//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by apple on 15/6/27.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置头像
        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
        
    }
    
 /// 在viewdidiappear里设置动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        UIView.animateWithDuration(1.2, animations: {
            self.view.layoutIfNeeded()
            }) { _  in
                UIView.animateWithDuration(1.2, animations: {
                    self.messageLabel.alpha = 1.0
                    }, completion: { _ in
                        NSNotificationCenter.defaultCenter().postNotificationName(LLSwitchRootVCNotification, object: true)                })
        }
        
        
    }
    
    
    
    /// 头像的底部约束
    var bottomConstraint: NSLayoutConstraint?

///  在loadView里设置控件布局
    override func loadView() {
        
        view = UIView()
        let backImageView = UIImageView(image: UIImage(named:  "ad_background"))
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        
        //自动布局
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        //水平方向
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : backImageView]))
        //垂直方向
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : backImageView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        bottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
        view.addConstraint(bottomConstraint!)
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView" : iconView]))

        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8))

    }
    
    
///  控件的懒加载
    lazy var iconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
    
    return iv
    
    }()
    lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "欢迎回来  \(sharedUserAccount!.name)"
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
        }()

    
}